var MainUserConsentSelectorManager=function(e){this.selectors=[];this.init=function(e){this.actionRequestUrl=e.actionRequestUrl;this.initSlider();var t=document.querySelectorAll("[data-bx-user-consent-selector]");t=BX.convert.nodeListToArray(t);t.forEach(this.initByContext,this)};this.initByContext=function(e){var t=e.querySelector("select[data-bx-selector]");var i=e.querySelector("a[data-bx-link-edit]");var n=e.querySelector("a[data-bx-link-view]");if(!t||!i||!n){return}this.selectors.push(t);BX.bind(t,"change",this.onChange.bind(this,t,i,n));this.onChange(t,i,n);this.initSliderButtons(e)};this.onChange=function(e,t,i){t.style.display=e.value?"":"none";i.style.display=e.value?"":"none";this.fillHrefByTemplate(t,e.value);this.fillHrefByTemplate(i,e.value)};this.fillHrefByTemplate=function(e,t){var i=e.getAttribute("data-bx-link-tmpl");if(!i){return}e.href=i.replace(new RegExp("#id#","g"),t)};this.fillDropDownControl=function(e,t,i){t=t||[];var n=e.children[0];e.innerHTML="";if(i&&n){e.appendChild(n)}t.forEach(function(t){if(!t||!t.caption){return}var i=document.createElement("option");i.value=t.value;i.selected=!!t.selected;i.innerText=t.caption;e.appendChild(i)})};this.initSliderButtons=function(e){var t=e.querySelectorAll("[data-bx-slider-href]");t=BX.convert.nodeListToArray(t);t.forEach(this.slider.bindOpen,this.slider)};this.initSlider=function(){this.slider.caller=this;top.BX.addCustomEvent(top,"main-user-consent-to-list",function(){top.BX.SidePanel.Instance.close()})};this.onSliderClose=function(){this.sendActionRequest("getAgreements",{},function(e){if(!e.list){return}this.selectors.forEach(function(t){var i=t.value;if(!i){i=e.list[0].ID}var n=e.list.map(function(e){return{caption:e.NAME,value:e.ID,selected:(e.ID||"").toString()===i}});this.fillDropDownControl(t,n,true);BX.fireEvent(t,"change")},this)})};this.slider={caller:null,init:function(e){top.BX.SidePanel.Instance.bindAnchors({rules:[{condition:e.condition,loader:e.loader,stopParameters:[]}]})},onSaved:function(){this.onClose();top.BX.SidePanel.Instance.close()},onClose:function(){if(this.caller){this.caller.onSliderClose()}},bindOpen:function(e){BX.bind(e,"click",this.openHref.bind(this,e))},openHref:function(e,t){t.preventDefault();this.open(e.getAttribute("href"),e.getAttribute("data-bx-slider-reload"))},open:function(e,t){top.BX.SidePanel.Instance.open(e,{cacheable:false,events:{}});if(t){top.BX.addCustomEvent(top,"main-user-consent-saved",this.onSaved.bind(this))}}};this.sendActionRequest=function(e,t,i,n){i=i||null;n=n||null;t.action=e;t.sessid=BX.bitrix_sessid();t.action=e;BX.ajax({url:this.actionRequestUrl,method:"POST",data:t,timeout:10,dataType:"json",processData:true,onsuccess:BX.proxy(function(e){e=e||{};if(e.error){n.apply(this,[e])}else if(i){i.apply(this,[e])}},this),onfailure:BX.proxy(function(){var e={error:true,text:""};if(n){n.apply(this,[e])}},this)})};this.init(e)};
//# sourceMappingURL=script.map.js